package com.bledanliaar.measure.biz;

import android.os.CountDownTimer;

import com.blelibrary.BleOperate;

/**
 * Created by My on 2017/4/25.
 */
public class BleData implements IBleData {
    int COUNT = 3;//重复请求指令的次数
    int LOOP_WAIT_TIME = 2000;//循环等待的时长
    CommandCountDownTimer commandCountDownTimer;
    CommandWaitCountDownTimer commandWaitCountDownTimer;

    public BleData() {

        commandCountDownTimer = new CommandCountDownTimer(LOOP_WAIT_TIME, 1000);
        commandWaitCountDownTimer = new CommandWaitCountDownTimer(getWaittime(), 1000);
    }


    @Override
    public void get33Command(String macAddress, BleCommandListener commandListener) {
        startWork(macAddress, C_33, commandListener);
    }

    @Override
    public void get34Command(String macAddress, BleCommandListener commandListener) {
        startWork(macAddress, C_34, commandListener);

    }

    @Override
    public void get35Command(String macAddress, BleCommandListener commandListener) {
        startWork(macAddress, C_35, commandListener);

    }

    @Override
    public void closeCommand(String macAddress) {
        BleOperate.getInstance().writeCharacteristic(macAddress, C_32);
    }

    @Override
    public void setWaitTime(int millis) {

    }

    @Override
    public int getWaittime() {
        return 10000;

    }

    /**
     * 设置波长，打开该波长灯管
     * 等待固定的（十秒）时间获取测量结果
     *
     * @param macAddress      mac地址
     * @param channel         波长指令
     * @param channelListener 返回值监听
     */
    public void startWork(String macAddress, byte[] channel, BleCommandListener channelListener) {
        BleOperate.getInstance().writeCharacteristic(macAddress, channel);
        commandWaitCountDownTimer.setParameter(macAddress, channelListener);
        commandWaitCountDownTimer.start();

    }

    /**
     * 根据打开的灯管获取波长的数据
     * 等待getWaittime()+LOOP_WAIT_TIME毫秒后 请求第一次数据
     * 若无数据返回则LOOP_WAIT_TIME后执行第二次总共执行COUNT次
     *
     * @param macAddress
     * @param commandListener
     */
    public void getDataByCommand(String macAddress, final BleCommandListener commandListener) {
        commandCountDownTimer.setParameter(macAddress, commandListener);
        commandCountDownTimer.start();
        BleOperate.getInstance().writeCharacteristic(macAddress, C_88);
    }

    class CommandWaitCountDownTimer extends CountDownTimer {

        String macAddress;
        BleCommandListener channelListener;

        public CommandWaitCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if (macAddress == null || channelListener == null) {
                throw new RuntimeException("计时器缺少参数");
            } else {
                getDataByCommand(macAddress, channelListener);
            }
        }

        public void setParameter(String macAddress, BleCommandListener channelListener) {
            this.macAddress = macAddress;
            this.channelListener = channelListener;
        }

        ;
    }

    class CommandCountDownTimer extends CountDownTimer {
        String macAddress;
        BleCommandListener channelListener;

        public CommandCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            if (macAddress == null || channelListener == null) {
                throw new RuntimeException("计时器缺少参数");
            }
            COUNT--;
//            Log.e("TAG", "COUNT-->" + COUNT);
            if (COUNT <= 0) {
                channelListener.error();
            } else {
                getDataByCommand(macAddress, channelListener);
            }

        }

        public void setParameter(String macAddress, BleCommandListener channelListener) {
            this.macAddress = macAddress;
            this.channelListener = channelListener;
        }

        ;
    }
}
